/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2020-06-09 17:12:04
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-06-10 19:43:51
 */
// http.js:
// const Promise = require('./es6-promise.js')  // 兼容低版本微信
const http = (url, data, type, headerCon) => {
    let userInfo = wx.getStorageSync('userInfo');
    return new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            method: type,
            header: {
                'Authorization': userInfo ? `${userInfo.userToken}` : '',
                'User-Xtj': 'X-XCX'
            },
            data: data,
            success: function (res) {
                if (res.statusCode != 200) {
                    wx.showToast({
                        title: '网络错误',
                        icon: 'none'
                    })
                    reject({ error: '服务器忙，请稍后重试', code: 500 });
                    return;
                }
                if (res.data.code == '-10') { //登录信息失效
                    wx.removeStorageSync('apiCode');
                    wx.removeStorageSync('userInfo');
                    wx.navigateTo({
                        url: `../login/login`
                    })
                    return;
                }
                resolve(res.data);
            },
            fail: function (err) {
                wx.showToast({
                    title: '网络错误',
                    icon: 'none'
                })
                // fail调用接口失败
                reject({ error: err, code: 0 });
            },
            complete: function (res) {
                // complete
            }
        })
    })
}

module.exports = http